package com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.nickstumpos.android.GmailWidgetPlus.R;
import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;

public abstract class OptionsList extends ListView {

	private ArrayAdapter<String>	aa;
	private final int				widgetId;

	public OptionsList(final Context context,
			final int widgetId,
			final String title) {

		super(context);
		this.widgetId = widgetId;
		this.initializeArrayAdaptor();
		final LayoutInflater lf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View v = lf.inflate(R.layout.choose_option_choice, null);
		final Button cancel = (Button) v.findViewById(R.id.CANCEL_OP);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {

				OptionsList.this.setVisibility(View.GONE);
			}
		});
		final Button ok = (Button) v.findViewById(R.id.OK_OP);
		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {

				OptionsList.this.save();
				OptionsList.this.setVisibility(View.GONE);
			}
		});
		this.addFooterView(v, null, false);
		final TextView tv = new TextView(context);
		tv.setText(title);
		tv.setTextSize((float) 20.0);
		tv.setGravity(Gravity.CENTER);
		this.addHeaderView(tv, null, false);
		this.setAdapter(this.getAa());
		this.requestFocus();
		this.setId(this.hashCode());
		this.setBackgroundColor(0xFF000000);
		this.setupDialog();
	}

	protected ArrayAdapter<String> getAa() {

		return this.aa;
	}

	/**
	 * @return the prefs
	 */
	protected SharedPreferences getPrefs() {

		return this.getContext()
				.getSharedPreferences(GMWP_Constants.GMW_PREFS_REF, 0);
	}

	protected int getWidgetId() {

		return this.widgetId;
	}

	protected void initializeArrayAdaptor() {

		this.setAa(new ArrayAdapter<String>(this.getContext(),
				android.R.layout.simple_list_item_checked));
	}

	@Override
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {

		boolean ret = false;
		if ((keyCode == KeyEvent.KEYCODE_BACK) && (this.getVisibility() == View.VISIBLE)) {
			new AlertDialog.Builder(this.getContext()).setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle(R.string.exit_without_saving_)
					.setMessage(R.string.return_to_main_menu_without_saving_)
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener() {

								@Override
								public void
										onClick(final DialogInterface dialog,
												final int which) {

									OptionsList.this.setVisibility(View.GONE);
								}
							})
					.setNegativeButton(R.string.save,
							new DialogInterface.OnClickListener() {

								@Override
								public void
										onClick(final DialogInterface dialog,
												final int which) {

									OptionsList.this.save();
									OptionsList.this.setVisibility(View.GONE);
								}
							})
					.show();
			ret = true;
		} else {
			ret = super.onKeyDown(keyCode, event);
		}
		return ret;
	}

	protected abstract void save();

	public void setAa(final ArrayAdapter<String> aa) {

		this.aa = aa;
	}

	protected abstract void setupDialog();
}
